package com.ericdm.listviewwithcheckbox;

import java.util.ArrayList;
import java.util.HashMap;
import com.ericdm.callingandsmsstatistics.R;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ListViewAdapterOutgoincCall extends BaseAdapter {
	private ArrayList<String> list;
	private ArrayList<String> callTimeList;
	private Context context;
	private LayoutInflater layoutInflater = null;
	public static HashMap<Integer, Boolean> isSelected;
	public static Boolean isShowCheckBox = false;

	@SuppressLint("UseSparseArrays")
	public ListViewAdapterOutgoincCall(ArrayList<String> list, ArrayList<String> callTimeList, Context context) {
		this.context = context;
		this.list = list;
		this.callTimeList = callTimeList;
		layoutInflater = LayoutInflater.from(context);
		isSelected = new HashMap<Integer, Boolean>();
		deselectAllItems();
	}
	
	public ArrayList<String> getSelectItems(){
		ArrayList<String> selectedPhoneNumber = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			if (getIsSelected().get(i)) {
				selectedPhoneNumber.add(list.get(i));
			}
		}
		return selectedPhoneNumber;
	}
	
	public void inverseSelectItems(){
		for (int i = 0; i < list.size(); i++) {
			if (getIsSelected().get(i)) {
				getIsSelected().put(i, false);
			}else {
				getIsSelected().put(i, true);
			}
		}
	}

	public void deselectAllItems() {
		for (int i = 0; i < list.size(); i++) {
			getIsSelected().put(i, false);
		}
	}
	
	public static boolean isItemSelected(int position){
		return isSelected.get(position);
	}

	public static HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void selectAllItems() {
		for (int i = 0; i < list.size(); i++) {
			getIsSelected().put(i, true);
		}
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.listview_checkbox, null);
			viewHolder.tv = (TextView) convertView.findViewById(R.id.listViewTextContent);
			viewHolder.tv_time = (TextView) convertView.findViewById(R.id.listViewTextContent_time);
			viewHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (isShowCheckBox) {
			viewHolder.cb.setVisibility(View.VISIBLE);
		} else {
			viewHolder.cb.setVisibility(View.INVISIBLE);
		}
		viewHolder.tv.setText((CharSequence) list.get(position));
		viewHolder.tv_time.setText((CharSequence) callTimeList.get(position));
		viewHolder.cb.setChecked((Boolean) getIsSelected().get(position));
		return convertView;
	}

	public static class ViewHolder {
		public TextView tv;
		public TextView tv_time;
		public CheckBox cb;
	}
}
